<template>
  <div id="container">
    <el-row>
      <el-col :span="12">
        <el-form ref="form" :model="income" label-width="80px">
          <el-form-item label="收入分类">
            <el-select v-model="income.incomeSort" placeholder="请选择">
              <el-option
                v-for="item in incomeSortList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="收入金额">
            <el-input v-model="income.incomeNum" style="width: 80px"></el-input>
          </el-form-item>
          <el-form-item label="收入时间">
            <el-col :span="11">
              <el-date-picker
                v-model="income.incomeTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期">
              </el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="用户编号">
            <el-input v-model="income.incomeUserId" style="width: 80px"></el-input>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="income.incomeUser" style="width: 120px"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" v-model="income.incomeRemark"></el-input>
          </el-form-item>
          <el-form-item label="上传凭证">
            <el-upload
              class="upload-demo"
              accept=".png,.jpg,.jpeg"
              action="#"
              ref="upload"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-change="handleChange"
              :file-list="fileList"
              :auto-upload="false">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
              <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "add",
  data() {
    return {
      fileList: [],
      incomeSortList: [
        {
          value: '工资收入',
          label: '工资收入'
        }, {
          value: '人情社交',
          label: '人情社交'
        }, {
          value: '兼职收入',
          label: '兼职收入'
        }, {
          value: '投资收入',
          label: '投资收入'
        },
        {
          value: '其它',
          label: '其它'
        }
      ],
      income: {
        //incomeId: 100,
        incomeTime: '',
        incomeNum: 0,
        incomeSort: '工资收入',
        incomeUserId: this.$store.state.user.uid,
        incomeRemark: '',
        incomeUser: this.$store.state.user.realName,
        voucherUrl: ''
      }
    }
  },
  mounted() {

  },
  methods: {
    handleChange(file, fileList) {
      this.fileList = fileList;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    submitUpload() {
      //this.$refs.upload.submit();
      if (this.$refs.upload.uploadFiles.length > 0) {
        const formData = new FormData();//  用FormData存放上传文件
        console.log("--------上传时this.fileList----------")
        console.log(this.fileList);

        this.fileList.forEach((file) => {
          formData.append("file", file.raw);
        });
        console.log("-------要上传的formData---------")
        console.log(formData)

        this.$store.dispatch('upload',formData).then(resp=>{
          //console.log(resp);
          if(resp.code === 200){
            this.$message({
              type: 'success',
              message: '文件上传成功！'
            })

            this.income.voucherUrl = resp.data;
          }
        })

      } else {
        this.$message({
          type: 'error',
          message: '请上传资源文件'
        })
        //this.$tool.message("请上传资源文件", "error");
      }
    },
    onSubmit() {
      //console.log('submit!');
      console.log("-------保存收入信息---------")
      console.log("-------要保存的收入信息详情如下--------")
      console.log(this.income);
      //调用保存支出信息的接口
      this.$store.dispatch('addIncome',this.income).then(resp=>{
        if(resp.code  === 200){
          this.$message({
            type: 'success',
            message: resp.msg
          })
        }
      })
    }
  }
}
</script>

<style scoped>
#container {
  margin: 20px 10px;
  padding-right: 20px;
}

.demo-form-inline {
  text-align: center;
}
</style>
